﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using HslCommunication.Controls;

namespace HslCommunication.BasicFramework
{
	/// <summary>
	/// 一个正方形图形选择窗口，可以获取指定的分辨率
	/// </summary>
	// Token: 0x02000187 RID: 391
	public partial class FormPortraitSelect : Form
	{
		/// <summary>
		/// 实例化一个对象
		/// </summary>
		// Token: 0x06001FD6 RID: 8150 RVA: 0x0009AB6F File Offset: 0x00098D6F
		public FormPortraitSelect()
		{
			this.InitializeComponent();
		}

		/// <summary>
		/// 是否有图片存在
		/// </summary>
		// Token: 0x17000695 RID: 1685
		// (get) Token: 0x06001FD7 RID: 8151 RVA: 0x0009AB9E File Offset: 0x00098D9E
		// (set) Token: 0x06001FD8 RID: 8152 RVA: 0x0009ABA6 File Offset: 0x00098DA6
		private bool HasPicture { get; set; }

		/// <summary>
		/// 已选择的图形大小
		/// </summary>
		// Token: 0x17000696 RID: 1686
		// (get) Token: 0x06001FD9 RID: 8153 RVA: 0x0009ABAF File Offset: 0x00098DAF
		// (set) Token: 0x06001FDA RID: 8154 RVA: 0x0009ABB7 File Offset: 0x00098DB7
		private Rectangle RectangleSelected { get; set; }

		// Token: 0x17000697 RID: 1687
		// (get) Token: 0x06001FDB RID: 8155 RVA: 0x0009ABC0 File Offset: 0x00098DC0
		// (set) Token: 0x06001FDC RID: 8156 RVA: 0x0009ABC8 File Offset: 0x00098DC8
		private Rectangle RectangleMoved { get; set; }

		/// <summary>
		/// 在控件显示的图片的大小，按照比例缩放以后
		/// </summary>
		// Token: 0x17000698 RID: 1688
		// (get) Token: 0x06001FDD RID: 8157 RVA: 0x0009ABD1 File Offset: 0x00098DD1
		// (set) Token: 0x06001FDE RID: 8158 RVA: 0x0009ABD9 File Offset: 0x00098DD9
		private Rectangle RectangleImage { get; set; }

		// Token: 0x06001FDF RID: 8159 RVA: 0x0009ABE2 File Offset: 0x00098DE2
		private void pictureBox1_MouseEnter(object sender, EventArgs e)
		{
			this.IsMouseOver = true;
			this.pictureBox1.Refresh();
		}

		// Token: 0x06001FE0 RID: 8160 RVA: 0x0009ABF9 File Offset: 0x00098DF9
		private void pictureBox1_MouseLeave(object sender, EventArgs e)
		{
			this.IsMouseOver = false;
			this.pictureBox1.Refresh();
		}

		// Token: 0x06001FE1 RID: 8161 RVA: 0x0009AC10 File Offset: 0x00098E10
		private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
		{
			bool flag = this.IsMouseOver && this.HasPicture;
			if (flag)
			{
				bool flag2 = this.RectangleSelected.Contains(e.Location);
				if (flag2)
				{
					this.Cursor = Cursors.SizeAll;
					bool isMouseDown = this.IsMouseDown;
					if (isMouseDown)
					{
						this.MoveByPoint(e.Location);
					}
				}
				else
				{
					this.Cursor = Cursors.Default;
				}
			}
		}

		// Token: 0x06001FE2 RID: 8162 RVA: 0x0009AC84 File Offset: 0x00098E84
		private void MoveByPoint(Point point)
		{
			bool flag = this.RectangleImage.Width >= this.RectangleImage.Height;
			if (flag)
			{
				int num = point.X - this.MouseMovePrecives.X;
				int num2 = num + this.RectangleSelected.X;
				bool flag2 = num2 >= 0 && num2 <= this.RectangleImage.Width - this.RectangleSelected.Width;
				if (flag2)
				{
					this.RectangleSelected = new Rectangle(num2, this.RectangleSelected.Y, this.RectangleSelected.Width, this.RectangleSelected.Height);
					this.pictureBox1.Refresh();
				}
			}
			else
			{
				int num3 = point.Y - this.MouseMovePrecives.Y;
				int num4 = num3 + this.RectangleSelected.Y;
				bool flag3 = num4 >= 0 && num4 <= this.RectangleImage.Height - this.RectangleSelected.Height;
				if (flag3)
				{
					this.RectangleSelected = new Rectangle(this.RectangleSelected.X, num4, this.RectangleSelected.Width, this.RectangleSelected.Height);
					this.pictureBox1.Refresh();
				}
			}
			this.MouseMovePrecives = point;
		}

		// Token: 0x06001FE3 RID: 8163 RVA: 0x0009AE10 File Offset: 0x00099010
		private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
		{
			bool flag = this.IsMouseOver && this.HasPicture;
			if (flag)
			{
				bool flag2 = this.RectangleSelected.Contains(e.Location);
				if (flag2)
				{
					this.IsMouseDown = true;
					this.MouseDownPoint = e.Location;
					this.MouseMovePrecives = e.Location;
				}
			}
			this.pictureBox1.Focus();
		}

		// Token: 0x06001FE4 RID: 8164 RVA: 0x0009AE80 File Offset: 0x00099080
		private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
		{
			bool hasPicture = this.HasPicture;
			if (hasPicture)
			{
				bool flag = this.RectangleSelected.Contains(e.Location);
				if (flag)
				{
					this.SetImgaeMiniShow();
				}
			}
			this.IsMouseDown = false;
			this.pictureBox1.Refresh();
		}

		// Token: 0x06001FE5 RID: 8165 RVA: 0x0009AED0 File Offset: 0x000990D0
		private void SetImgaeMiniShow()
		{
			Rectangle des = this.RectangleRestore();
			Image image = this.pictureBox2.Image;
			if (image != null)
			{
				image.Dispose();
			}
			this.pictureBox2.Image = this.GetSpecicalSizeFromImage(100, des);
			Image image2 = this.pictureBox3.Image;
			if (image2 != null)
			{
				image2.Dispose();
			}
			this.pictureBox3.Image = this.GetSpecicalSizeFromImage(64, des);
			Image image3 = this.pictureBox4.Image;
			if (image3 != null)
			{
				image3.Dispose();
			}
			this.pictureBox4.Image = this.GetSpecicalSizeFromImage(32, des);
		}

		// Token: 0x06001FE6 RID: 8166 RVA: 0x0009AF6C File Offset: 0x0009916C
		private Rectangle RectangleRestore()
		{
			int width = this.pictureBox1.Image.Width;
			int height = this.pictureBox1.Image.Height;
			bool flag = width < height;
			Rectangle result;
			if (flag)
			{
				result = new Rectangle(0, this.RectangleSelected.Y * height / 370, width, width);
			}
			else
			{
				result = new Rectangle(this.RectangleSelected.X * width / 370, 0, height, height);
			}
			return result;
		}

		// Token: 0x06001FE7 RID: 8167 RVA: 0x0009AFEC File Offset: 0x000991EC
		private Bitmap GetSpecicalSizeFromImage(int size, Rectangle des)
		{
			Bitmap bitmap = new Bitmap(size, size);
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				graphics.DrawImage(this.pictureBox1.Image, new Rectangle(0, 0, size, size), des, GraphicsUnit.Pixel);
			}
			return bitmap;
		}

		// Token: 0x06001FE8 RID: 8168 RVA: 0x0009B04C File Offset: 0x0009924C
		private void userButton1_Click(object sender, EventArgs e)
		{
			using (OpenFileDialog openFileDialog = new OpenFileDialog())
			{
				openFileDialog.Multiselect = false;
				openFileDialog.Title = "请选择一张图片";
				openFileDialog.Filter = "图片文件(*.jpg)|*.jpg|图片文件(*.png)|*.png";
				bool flag = openFileDialog.ShowDialog() == DialogResult.OK;
				if (flag)
				{
					this.LoadPictureFile(openFileDialog.FileName);
				}
			}
		}

		/// <summary>
		/// 增加一张图片的路径
		/// </summary>
		/// <param name="picPath"></param>
		// Token: 0x06001FE9 RID: 8169 RVA: 0x0009B0BC File Offset: 0x000992BC
		private void LoadPictureFile(string picPath)
		{
			Bitmap bitmap = null;
			try
			{
				bitmap = (Bitmap)Image.FromFile(picPath);
				int num = bitmap.Width;
				int num2 = bitmap.Height;
				bool flag = num > num2;
				if (flag)
				{
					num2 = num2 * 370 / num;
					num = 369;
				}
				else
				{
					num = num * 370 / num2;
					num2 = 369;
				}
				this.label1.Text = string.Format("({0},{1})", num, num2);
				this.RectangleImage = new Rectangle((370 - num) / 2, (370 - num2) / 2, num, num2);
				bool flag2 = this.RectangleImage.Width >= this.RectangleImage.Height;
				if (flag2)
				{
					this.RectangleSelected = new Rectangle(this.RectangleImage.X, this.RectangleImage.Y, this.RectangleImage.Height, this.RectangleImage.Height);
				}
				else
				{
					this.RectangleSelected = new Rectangle(this.RectangleImage.X, this.RectangleImage.Y, this.RectangleImage.Width, this.RectangleImage.Width);
				}
				this.HasPicture = true;
				this.pictureBox1.Refresh();
			}
			catch (Exception ex)
			{
				SoftBasic.ShowExceptionMessage(ex);
				return;
			}
			this.pictureBox1.Image = bitmap;
			this.SetImgaeMiniShow();
		}

		// Token: 0x06001FEA RID: 8170 RVA: 0x00009AA1 File Offset: 0x00007CA1
		private void FormPortraitSelect_Load(object sender, EventArgs e)
		{
		}

		// Token: 0x17000699 RID: 1689
		// (get) Token: 0x06001FEB RID: 8171 RVA: 0x0009B26C File Offset: 0x0009946C
		// (set) Token: 0x06001FEC RID: 8172 RVA: 0x0009B274 File Offset: 0x00099474
		private bool IsMouseOver { get; set; }

		// Token: 0x1700069A RID: 1690
		// (get) Token: 0x06001FED RID: 8173 RVA: 0x0009B27D File Offset: 0x0009947D
		// (set) Token: 0x06001FEE RID: 8174 RVA: 0x0009B285 File Offset: 0x00099485
		private bool IsMouseOverOnImage { get; set; }

		// Token: 0x1700069B RID: 1691
		// (get) Token: 0x06001FEF RID: 8175 RVA: 0x0009B28E File Offset: 0x0009948E
		// (set) Token: 0x06001FF0 RID: 8176 RVA: 0x0009B296 File Offset: 0x00099496
		private bool IsMouseDown { get; set; }

		// Token: 0x1700069C RID: 1692
		// (get) Token: 0x06001FF1 RID: 8177 RVA: 0x0009B29F File Offset: 0x0009949F
		// (set) Token: 0x06001FF2 RID: 8178 RVA: 0x0009B2A7 File Offset: 0x000994A7
		private Point MouseDownPoint { get; set; }

		// Token: 0x1700069D RID: 1693
		// (get) Token: 0x06001FF3 RID: 8179 RVA: 0x0009B2B0 File Offset: 0x000994B0
		// (set) Token: 0x06001FF4 RID: 8180 RVA: 0x0009B2B8 File Offset: 0x000994B8
		private Point MouseMovePrecives { get; set; }

		// Token: 0x06001FF5 RID: 8181 RVA: 0x0009B2C4 File Offset: 0x000994C4
		private void pictureBox1_Paint(object sender, PaintEventArgs e)
		{
			bool hasPicture = this.HasPicture;
			if (hasPicture)
			{
				Graphics graphics = e.Graphics;
				graphics.FillRectangle(this.brush, this.RectangleSelected);
				graphics.DrawRectangle(Pens.LightSkyBlue, this.RectangleSelected);
			}
		}

		// Token: 0x06001FF6 RID: 8182 RVA: 0x0009B30C File Offset: 0x0009950C
		private void userButton2_Click(object sender, EventArgs e)
		{
			bool hasPicture = this.HasPicture;
			if (hasPicture)
			{
				base.DialogResult = DialogResult.OK;
			}
			else
			{
				base.DialogResult = DialogResult.Cancel;
			}
		}

		// Token: 0x06001FF7 RID: 8183 RVA: 0x00009AA1 File Offset: 0x00007CA1
		private void pictureBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
		}

		/// <summary>
		/// 获取指定大小的图片，该图片将会按照比例压缩
		/// </summary>
		/// <param name="size">图片的横向分辨率</param>
		/// <returns>缩放后的图形</returns>
		// Token: 0x06001FF8 RID: 8184 RVA: 0x0009B33C File Offset: 0x0009953C
		public Bitmap GetSpecifiedSizeImage(int size)
		{
			bool hasPicture = this.HasPicture;
			Bitmap result;
			if (hasPicture)
			{
				result = this.GetSpecicalSizeFromImage(size, this.RectangleRestore());
			}
			else
			{
				result = null;
			}
			return result;
		}

		// Token: 0x0400077D RID: 1917
		private Brush brush = new SolidBrush(Color.FromArgb(120, Color.Gray));
	}
}
